﻿//准备ExtJS环境
Ext.require
(
    [
        'Ext.form.field.File',
        'Ext.grid.*',
        'Ext.data.*',
        'Ext.util.*',
        'Ext.state.*',
        'Ext.form.*'
    ]
);

//定义用户表格控件
Ext.define
(
    'Instruction.Grid',
    {
        extend: 'Ext.grid.Panel',

        //检索窗体
        win_search: null,

        //初始化组件
        initComponent: function () {
            Ext.apply
            (
                this,
                {
                    listeners:
                    {
                        //设置两个指头的滚动
                        render: Common.ScrollableRender
                    },
                    dockedItems:
                    [
                        {
                            xtype: 'toolbar',
                            height: 36,
                            items:
                            [
                                {
                                    iconCls: 'icon-refresh',
                                    itemId: 'button-refresh',
                                    text: '刷新',
                                    scope: this,
                                    hidden: false,
                                    tooltip: '刷新数据',
                                    handler: this.onRefreshClick
                                },
                                '-',
                                {
                                    iconCls: 'icon-view',
                                    itemId: 'button-view',
                                    text: '查看',
                                    scope: this,
                                    disabled: true,
                                    hidden: false,
                                    tooltip: '查看指令',
                                    handler: this.onViewClick
                                }
                            ]
                        }
                    ]
                }
            );
            this.callParent();
            this.getSelectionModel().on('selectionchange', this.onSelectionChange, this);
        },

        //处理选中变化事件
        onSelectionChange: function (selectionModel, selections) {
            //判断是否选中突发事件
            if (selections.length === 0) {
                //禁用查看按钮
                this.down('#button-view').setDisabled(true);
            }
            else {
                //启用查看按钮
                this.down('#button-view').setDisabled(false);
            }
        },

        //处理刷新按钮点击事件
        onRefreshClick: function () {
            //刷新数据
            this.store.load();
        },

        //处理查看按钮点击事件
        onViewClick: function () {
            //获得选中的行
            var selection = this.getView().getSelectionModel().getSelection();

            //判断是否选中行
            if (selection.length > 0) {
                //查看工程详细信息
                window.open('/Instruction/ViewInstruction?instructionID=' + selection[0].data.InstructionID);
            }
        }
    }
);

//挂接环境准备完毕事件
Ext.onReady
(
    function () {
        //初始化快速提示
        Ext.QuickTips.init();

        //设置状态保存提供者（当前设置为Cookie）
        Ext.state.Manager.setProvider(Ext.create('Ext.state.CookieProvider', { path: _Current_ActionUrl }));

        //初始化附加验证类型
        Common.InitAdditionalValidation();

        //创建数据存储对象
        var store = Ext.create
        (
            'Ext.data.Store',
            {
                pageSize: 20,
                autoSync: true,
                autoLoad: true,
                root: 'data',
                remoteFilter: true,
                remoteSort: true,
                model: 'InstructionModel',
                proxy:
                {
                    type: 'ajax',
                    batchActions: false,
                    url: '/Instruction/Select',
                    reader:
                    {
                        type: 'json',
                        root: 'data'
                    },
                    listeners:
                    {
                        exception: function (proxy, response, operation, options) {
                            //处理错误
                            Common.HandleProxyError(proxy, response, operation, options);
                        }
                    }
                }
            }
        );

        Ext.create
        (
            'Instruction.Grid',
            {
                store: store,
                stateful: true,
                stateId: 'instruction-state-grid',
                height: 600,
                width: '100%',
                id: 'instruction-grid',
                title: '',
                renderTo: 'data-grid',
                columnLines: true,
                viewConfig:
                {
                    stripeRows: true,
                    loadingText: '数据加载中...',
                    singleSelect: true
                },
                //创建分页工具栏
                bbar: Ext.create
                (
                    'Ext.PagingToolbar',
                    {
                        store: store,
                        displayInfo: true,
                        displayMsg: '第 {0} - 第 {1} 条记录，共 {2} 条记录',
                        emptyMsg: ''
                    }
                ),
                columns:
                [
                    {
                        text: '指令主题',
                        width: 240,
                        sortable: false,
                        align: 'left',
                        dataIndex: 'Title',
                        renderer: Common.RenderInstructionTitle,
                        hideable: false
                    },
                    {
                        text: '指令类型',
                        width: 80,
                        sortable: false,
                        hideable: false,
                        align: 'center',
                        renderer: Common.RenderInstructionType,
                        dataIndex: 'InstructionType'
                    },
                    {
                        text: '发布人',
                        width: 120,
                        sortable: false,
                        hideable: false,
                        align: 'left',
                        dataIndex: 'Creater'
                    },
                    {
                        text: '发布日期',
                        width: 80,
                        sortable: true,
                        hideable: false,
                        align: 'right',
                        renderer: Ext.util.Format.dateRenderer('Y/m/d'),
                        dataIndex: 'CreateDate'
                    },
                    {
                        text: '',
                        flex: true,
                        sortable: false,
                        align: 'left',
                        hideable: false
                    }
                ]
            }
        );

        $(window).bind
        (
            "resize",
            function () {
                //调整表格宽度
                Common.AdjustGridWidth('instruction-grid', 'body_content');
            }
        );

        //调整表格宽度
        Common.AdjustGridWidth('instruction-grid', 'body_content');
    }
);
